package com.maoye.ossp.biz.brand.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.brand.entity.SupplierBrand;
import com.maoye.ossp.biz.brand.entity.SupplierReview;
import com.maoye.ossp.biz.brand.service.ISupplierBrandService;
import com.maoye.ossp.biz.brand.service.ISupplierReviewService;
import com.maoye.ossp.biz.brand.service.ISupplierShopService;
import com.maoye.ossp.biz.brand.utils.BrandCommonConstant;
import com.maoye.ossp.biz.brand.utils.BrandUtils;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:品牌评审记录信息 控制器类 开发公司:maoye shenzhen 开发人员:harry 创建时间:2013-09-30 11:37:20
 */
@Controller
@RequestMapping("/biz/brand/supplierReview/")
public class SupplierReviewController extends BaseController {
	@Resource
	private ISupplierReviewService supplierReviewService;
	@Resource
	private ISupplierBrandService supplierBrandService;
	@Resource
	private ISupplierShopService supplierShopService;

	/**
	 * 供应商评审记录列表
	 * 
	 * @param supplier
	 *            供应商评审记录实体
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "list")
	public String list(SupplierReview supplierReview, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, supplierReview);
		Page<SupplierReview> page = supplierReviewService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/brand/supplierReview/list", model, request);
	}

	/**
	 * 添加或者修改供应商评审结论
	 * 
	 * @param supplier
	 *            供应商评审记录实体
	 * @param model 
	 * @param redirectAttributes
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(SupplierReview supplierReview,Model model, RedirectAttributes redirectAttributes) {
		try
		{
			String msg = supplierReviewService.updateOrAdd(supplierReview);
			if(null != msg && !"".equals(msg))
			{
				addMessage(model, msg);
				model.addAttribute("supplierReview", supplierReview);
				return "/biz/brand/supplierReview/add";
			}
			addMessage(model, BrandCommonConstant.MSG_2002);
		}
		catch(Exception e)
		{
			addMessage(model, BrandCommonConstant.EX_1009);
		}
		return "redirect:/biz/brand/supplierReview/list";
	}

	/**
	 * 修改供应商评审记录
	 * 
	 * @param id
	 *            供应商评审记录ID
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model) throws Exception 
	{
		SupplierReview supplierReview=supplierReviewService.get(Long.valueOf(id));
		
		//List<SupplierShop> supplierShopList=supplierShopService.getBySupplierBrandId(supplierReview.getSupplierBrandId());
		SupplierBrand sb=supplierBrandService.getSupplierBrandValue(supplierReview);
		model.addAttribute("sb", sb);
		//model.addAttribute("supplierShopList", supplierShopList);
		model.addAttribute("supplierReview", supplierReview);
		return "/biz/brand/supplierReview/add";
	}

	@RequestMapping(value = "add")
	public String add(SupplierReview supplierReview, Model model)  throws Exception 
	{
		model.addAttribute("supplierReview", supplierReview);
		return "/biz/brand/supplierReview/add";
	}
	/**
	 * 查看供应商评审记录
	 * 
	 * @param id
	 *            供应商评审记录id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model) {
		SupplierReview supplierReview=supplierReviewService.get(Long.valueOf(id));
		SupplierBrand supplierBrand=supplierBrandService.getSupplierBrandValue(supplierReview);
		model.addAttribute("supplierReview", supplierReview);
		model.addAttribute("supplierBrand", supplierBrand);
		return "/biz/brand/supplierReview/view";
	}
	/**
	 * 删除评审记录
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) {
			supplierReviewService.delByPK(Long.valueOf(id));	
		return "redirect:/biz/brand/supplierReview/list";
	}
	
	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) 
	{
		String deleteIds = request.getParameter("deleteIds");
		Long[] lAryId = BrandUtils.getLongAryByStr(deleteIds,";");
		for(Long id : lAryId)
		{
			supplierReviewService.delByPK(id);
		}
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/supplierReview/list";
	}
	
	@RequestMapping(value = "{id}/getShopList")
	public void getShopList(@PathVariable String id,Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception 
	{
		String jsonStr = supplierReviewService.getShopList(id);
        response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		out.write(jsonStr);
	}
}
